package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.missilike.core.domain.DeleteContentReuquest;
import com.missilike.core.domain.ForwardContentRequest;
import com.missilike.core.exception.SystemException;
import com.missilike.core.result.BaseResultCode;
import com.missilike.core.result.BizResultCode;
import com.missilike.repository.entity.MilAccountContent;
import com.missilike.repository.entity.MilContentTransmit;
import com.missilike.repository.mapper.MilContentTransmitMapper;
import com.missilike.biz.IMilContentTransmitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pengpengli
 * @since 2018-12-19
 */
@Service
public class MilContentTransmitServiceImpl extends ServiceImpl<MilContentTransmitMapper, MilContentTransmit> implements IMilContentTransmitService {

    private final MilAccountContentServiceImpl contentService;

    public MilContentTransmitServiceImpl(MilAccountContentServiceImpl contentService) {
        this.contentService = contentService;
    }

    @PostConstruct
    public void init() {
        contentService.setContentTransmitService(this);
    }

    public boolean forwardContent(ForwardContentRequest request) {
        healthCheckContent(request.getAuthorAccountId(), request.getContentId());
        this.save(new MilContentTransmit(
                request.getForwardAccountId(),
                request.getContentId(),
                LocalDateTime.now(),
                request.getContentType(),
                request.getContent())
        );
        return true;
    }

    public boolean deleteContentTransmit(@RequestBody DeleteContentReuquest reuquest) {
        MilContentTransmit milContentTransmit = getOne(new QueryWrapper<MilContentTransmit>().lambda()
                .eq(MilContentTransmit::getAccountId, reuquest.getAuthorAccountId())
                .eq(MilContentTransmit::getContentId, reuquest.getContentId()));
        if (milContentTransmit == null) {
            throw new SystemException(BizResultCode.ARTICLE_CONTENT_EXISTENT.getCode(), BizResultCode.ARTICLE_CONTENT_EXISTENT.getMsg());
        }
        this.removeById(milContentTransmit.getId());
        return true;
    }

    public MilAccountContent healthCheckContent(String AccountId, Long contentId) {
        MilAccountContent milAccountContent = contentService.getOne(new QueryWrapper<MilAccountContent>().lambda()
                .eq(MilAccountContent::getAccountId, AccountId)
                .eq(MilAccountContent::getId, contentId)
        );
        if (milAccountContent == null) {
            throw new SystemException(BizResultCode.ARTICLE_CONTENT_EXISTENT.getCode(), BizResultCode.ARTICLE_CONTENT_EXISTENT.getMsg());
        }
        return milAccountContent;
    }


}
